package dbs

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"go.uber.org/zap"
	"strconv"
	"yundian/internel/pkg/dingo/global"
)

type RedisComponent struct {
	cfg *global.RedisConfig
}

func NewRedisComponent() *RedisComponent {
	return &RedisComponent{cfg: global.Cfg.Redis}
}

func (r *RedisComponent) Step() {
	global.Rdb = redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%s", r.cfg.Host, strconv.Itoa(r.cfg.Port)),
		Password: r.cfg.Password, // no password set
		DB:       r.cfg.Db,  // use default DB
	})
	_,err := global.Rdb.Ping(context.Background()).Result()
	if err != nil {
		zap.L().Fatal("Redis 连接初始化失败",zap.Error(err))
	}
}